package Impl;

import Util.JDBCUtil;
import entity.Message;
import sun.management.Sensor;

import java.lang.management.MemoryManagerMXBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MessageDaoImpl implements MessageDao {
    UserDaoImpl userDao = new UserDaoImpl();
    Connection conn=null;
    PreparedStatement ps =null;
    ResultSet rs = null;

    @Override
    public void delete(int mid) {
        String sql = "delete from message where mid = ?";
        userDao.commonUp(sql,mid);
        System.out.println("删除短消息成功");
    }

    @Override
    public void add(Message message) {
        String  sql = "INSERT INTO message(recipient,content,sender,isRead) VALUES(?,?,?,?)";
        Object[] param = {message.getRecipient(),message.getContent(), message.getSender(),message.getIsRead()};
        userDao.commonUp(sql,param);
        System.out.println("成功添加短消息！");
    }

    @Override
    public List<Message> findMessageByrecipient(String recipient) {
        List<Message> messageList = new ArrayList<>();
        String sql="select * from message where recipient='"+recipient+"'";
        System.out.println(sql);
        Message m =null;
        try {
            conn = JDBCUtil.getConnection();
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                m=new Message();
                m.setRecipient(rs.getString("recipient"));
                m.setSender(rs.getString("sender"));
                m.setContent(rs.getString("content"));
                m.setIsRead(rs.getInt("isRead"));
                m.setMid(rs.getInt("mid"));
                messageList.add(m);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeAll(conn,ps,rs);
        }
        return messageList;
    }

    @Override
    public List<Message> findMessageBysender(String sender) {
        List<Message> messageList = new ArrayList<>();
        String sql="select * from message where sender='"+sender+"'";
        System.out.println(sql);
        Message m =null;
        try {
            conn = JDBCUtil.getConnection();
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                m=new Message();
                m.setRecipient(rs.getString("recipient"));
                m.setSender(rs.getString("sender"));
                m.setContent(rs.getString("content"));
                m.setIsRead(rs.getInt("isRead"));
                m.setMid(rs.getInt("mid"));
                messageList.add(m);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeAll(conn,ps,rs);
        }
        return messageList;
    }

    @Override
    public Message findMessageById(int mid,String recipient) {
        Message message = new Message();
        String sql = "select * from message where mid=? and recipient=?";
        conn=JDBCUtil.getConnection();
        try {
            ps=conn.prepareStatement(sql);
            ps.setObject(1,mid);
            ps.setObject(2,recipient);
            rs=ps.executeQuery();
            if (rs.next()){
                message.setMid(rs.getInt("mid"));
                message.setRecipient(rs.getString("recipient"));
                message.setSender(rs.getString("sender"));
                message.setContent(rs.getString("content"));
                message.setIsRead(rs.getInt("isRead"));
            }else{
                message=null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.closeAll(conn,ps,rs);
        }
        return message;
    }


    public static void main(String[] args) {
//        MessageDaoImpl messageDao = new MessageDaoImpl();
//        String recipient, String content, String sender, int isRead
//        Message m = new Message("花城","你好呀！","小小怪",1);
//        messageDao.add(m);
//        List<Message> list = messageDao.findMessageByrecipient("vivi");
//        System.out.println(list);
    }
}
